package com.luxury.activity;

import android.content.Context;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.annotation.IdRes;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.luxury.R;
import com.luxury.SlidingMenu;
import com.luxury.fragment.CategoryFragment;
import com.luxury.fragment.HomeFragment;
import com.luxury.fragment.MineFragment;
import com.luxury.fragment.PartyFragment;
import com.luxury.fragment.ShoppingbagFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity1 extends AppCompatActivity {
    private static final String TAG = "mainActivity";
    @BindView(R.id.radiogroup_main)
    RadioGroup radiogroup_main;
    @BindView(R.id.slidingmenu_main)
    SlidingMenu slidingmenuMain;
    private Context mContext = this;
    private List<Fragment> mList_fragment;
    private FragmentManager mManager;
    private FragmentTransaction mFt;
    private static final String SELINDEX = "INDEX";
    private int mSelIndext = 0;
    private Fragment home_fragment, party_fragment, category_fragment, shoppingbag_fragment,
            mine_fragment;
    private long mPressedTime = 0;//记录点击返回键的毫秒(双击返回键退出程序)
    private String[] tags = {"HOME", "PARTY", "CATEGORY", "SHOPPING", "MINE"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);

        //初始化所需要的fragment并将其加入到list中
        initFragment(savedInstanceState);
        Log.i(TAG, "list长度-->>" + mList_fragment.size());
        //对按钮设置监听
        radiogroup_main.setOnCheckedChangeListener(new ChangeListener());
    }

    public void setScrollEnable(boolean scrollEnable) {
        slidingmenuMain.setScrollEnable(scrollEnable);
    }

    class ChangeListener implements RadioGroup.OnCheckedChangeListener {
        @Override
        public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
            switch (checkedId) {
                case R.id.rb_home_main:
                    mSelIndext = 0;
                    break;
                case R.id.rb_party_main:
                    mSelIndext = 1;
                    break;
                case R.id.rb_category_main:
                    mSelIndext = 2;
                    break;
                case R.id.rb_shoppingbag_main:
                    mSelIndext = 3;
                    break;
                case R.id.rb_mine_main:
                    mSelIndext = 4;
                    break;
            }
            switchFragment(mManager, mList_fragment, mSelIndext, R.id.framgent_layout_main);
        }
    }

    @Override
    public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
        super.onSaveInstanceState(outState, outPersistentState);
        Log.i(TAG, "开始没啊???-->" + mSelIndext);
        outState.putInt(SELINDEX, mSelIndext);
        Log.i(TAG, "存没存啊???-->" + mSelIndext);

    }

    //--------------(封装的方法)初始化所需要的fragment并将其加入到list中-----------------------
    private void initFragment(Bundle savedInstanceState) {
        mManager = getSupportFragmentManager();
        Log.i(TAG, "initFragment:--->> " + mSelIndext);
        Log.i(TAG, "get到没??--->>" + mSelIndext);
        if (savedInstanceState != null) {
            mSelIndext = savedInstanceState.getInt(SELINDEX, mSelIndext);
            if (mManager.findFragmentByTag(tags[0]) != null) {
                home_fragment = (HomeFragment) mManager.findFragmentByTag(tags[0]);
            }else {
                home_fragment = HomeFragment.newInstance();
            }
            if(mManager.findFragmentByTag(tags[1])!=null){
                party_fragment = (PartyFragment) mManager.findFragmentByTag(tags[1]);
            }else {
                party_fragment = PartyFragment.newInstance();
            }
            if(mManager.findFragmentByTag(tags[2])!=null){
                category_fragment = (CategoryFragment) mManager.findFragmentByTag(tags[2]);
            }else {
                category_fragment = CategoryFragment.newInstance();            }
            if(mManager.findFragmentByTag(tags[3])!=null){
                shoppingbag_fragment = (ShoppingbagFragment) mManager.findFragmentByTag(tags[3]);
            }else {
                shoppingbag_fragment = ShoppingbagFragment.newInstance();
            }
            if(mManager.findFragmentByTag(tags[4])!=null){
                mine_fragment = (MineFragment) mManager.findFragmentByTag(tags[4]);
            }else {
                mine_fragment = MineFragment.newInstance();
            }
        } else {
            //首页的fragment
            home_fragment = HomeFragment.newInstance();
            //尖货的fragment
            party_fragment = PartyFragment.newInstance();
            //分类的fragment
            category_fragment = CategoryFragment.newInstance();
            //购物袋的fragment
            shoppingbag_fragment = ShoppingbagFragment.newInstance();
            //我的寺库的fragment
            mine_fragment = MineFragment.newInstance();
        }
        //存放fragment的集合 并将fragment放入集合中
        mList_fragment = new ArrayList<>();
        mList_fragment.add(home_fragment);
        mList_fragment.add(party_fragment);
        mList_fragment.add(category_fragment);
        mList_fragment.add(shoppingbag_fragment);
        mList_fragment.add(mine_fragment);
        //        首先让首页的fragment展示出来
        switchFragment(mManager, mList_fragment, 0, R.id.framgent_layout_main);
    }

    //-------------------(封装方法)选择fragment的方法(fragment 都加在出来  然后不用的隐藏)----------------------------
    private void switchFragment(FragmentManager manager, List<Fragment> list_fragment, int
            tabIndex, @IdRes int layoutId) {
        FragmentTransaction transaction = manager.beginTransaction();
        //       循环遍历吧所有的fragment隐藏起来
        for (int i = 0; i < list_fragment.size(); i++) {
            if (list_fragment.get(i).isVisible()) {
                transaction.hide(list_fragment.get(i));
            }
        }
        //        判断需要哪个fragment  然后如果没有的 将这个fragment添加 如果有的 将fragment显示出来
        Log.i(TAG, "点击下标-->>" + tabIndex);
        Fragment fragment = list_fragment.get(tabIndex);
        if (fragment.isAdded()) {
            transaction.show(fragment);
        } else {
            transaction.add(layoutId, fragment, tags[tabIndex]);
        }
        transaction.commit();
    }

    /**
     * 双击返回键退出程序
     */
    @Override
    public void onBackPressed() {
        long mNowTime = System.currentTimeMillis();//获取第一次按键时间
        if ((mNowTime - mPressedTime) > 2000) {//比较两次按键时间差
            Toast.makeText(this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
            mPressedTime = mNowTime;
        } else {
            //退出程序
            this.finish();
            System.exit(0);
        }
    }

/*    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
     *//*   Bundle bundle=new Bundle();
        bundle.putInt("keyCode",keyCode);
        bundle.putParcelable("keyevent",event);
        globalFragment.setArguments(bundle);*//*
        if (keyCode==KeyEvent.KEYCODE_BACK) {
        	GlobalFragment fragment=new GlobalFragment();
            WebView webView = (WebView) fragment.getView().findViewById(R.id
            .webView_globalFragment_h5);
            if(webView.canGoBack())
            {
                webView.goBack();
            }

        }
        return super.onKeyDown(keyCode, event);
    }*/
}
